#!/bin/sh
#########################
uninstall_server()
#########################
{
echo>> $LOG_FILE
echo " Checking if instance $ORACLE_SID is up ...     " | tee -a  $LOG_FILE
echo>> $LOG_FILE

is_running=`ps -ef|grep -iw ora_pmon_$ORACLE_SID|grep -iw $current_user |grep -v grep`

echo " ORACLE_SID : $ORACLE_SID" >> $LOG_FILE
echo "is running : $is_running " >> $LOG_FILE

if [ -z "$is_running" ]; then
# instance is down - start it
     start_inst
     if [ $? -eq 0 ]; then
        get_db_files
     else
# shutdown the db
       stop_inst
       stop_listener
     fi 
else
# instance is running under this account
     get_db_files
fi
uninstall_files
rc=$?
return $rc
#
}

#######################
uninstall_files()
#######################
{
  echo>> $LOG_FILE
  echo " Removing Oracle directory and pendants ...     " | tee -a  $LOG_FILE
  echo>> $LOG_FILE
  if [ -d $HOME/DB_MAINT ]; then
	rm -rf $HOME/DB_MAINT
	check_cmd "rm -rf $HOME/DB_MAINT"
  fi
  if [ -f $HOME/.oraenv.csh ]; then
	rm -f $HOME/.oraenv.csh 
	check_cmd "rm -f $HOME/.oraenv.csh"
  fi
  if [ -f $HOME/.oraenv.sh ]; then
	rm -f $HOME/.oraenv.sh
	check_cmd "rm -f $HOME/.oraenv.sh"
  fi
  if [ -f $HOME/stoplsnr.log ]; then
	mv $HOME/stoplsnr.log $uninstall_dir
	check_cmd "mv $HOME/stoplsnr.log $uninstall_dir"
  fi
  if [ -f $HOME/stopdb.log ]; then
	mv $HOME/stopdb.log $uninstall_dir
	check_cmd "mv $HOME/stopdb.log $uninstall_dir"
  fi
  if [ -f $HOME/startdb.log ]; then
        mv $HOME/startdb.log $uninstall_dir
	check_cmd "mv $HOME/startdb.log $uninstall_dir"
  fi
  if [ -f $HOME/get_db_files.log ]; then
        mv $HOME/get_db_files.log $uninstall_dir
	check_cmd "mv $HOME/get_db_files.log $uninstall_dir"
  fi
  if [ -f $HOME/rmfiles.sh ]; then
        mv $HOME/rmfiles.sh $uninstall_dir
	check_cmd "mv $HOME/rmfiles.sh $uninstall_dir"
  fi
  if [ -d $HOME/oracle ]; then
	rm -rf $HOME/oracle
	check_cmd "rm -rf $HOME/oracle"
  fi
}

#######################
get_db_files()
#######################
{
  echo>> $LOG_FILE
  echo " Retrieving database file names ...             " | tee -a  $LOG_FILE
  echo>> $LOG_FILE

 if [ -f $HOME/rmfiles.sh ]; then
    rm -f $HOME/rmfiles.sh
 fi
 run_get_db
 rc=$?
 # shutdown the db
 stop_inst
 stop_listener
 if [ $rc -eq 0 ]; then
     delete_db_files
  fi
}
####################
run_get_db()
####################
{
$ORACLE_HOME/bin/sqlplus -S '/ as sysdba'  <<GDB 1>$HOME/get_db_files.log 2>$HOME/get_db_files.log
set heading off
set linesize 80
set feedback off
set echo off
spool $HOME/rmfiles.sh
select '#!/bin/sh' from dual
/
select 'if [ -f '||name||' ] '||chr(10)||'then'||chr(10)||'rm -f '||name||chr(10)||'fi'
from v\$datafile
/
select 'if [ -f '||member||' ] '||chr(10)||'then'||chr(10)||'rm -f '||member||chr(10)||'fi'
from v\$logfile
/
select 'if [ -f '||name||' ] '||chr(10)||'then'||chr(10)||'rm -f '||name||chr(10)||'fi'
from v\$tempfile
/
select 'if [ -f '||name||' ] '||chr(10)||'then'||chr(10)||'rm -f '||name||chr(10)||'fi'
from v\$controlfile
/
spool off 
exit 

GDB

rc=$?
st=`cat $HOME/get_db_files.log | grep "ORA-"`
if [ -n "$st" -o $rc -ne 0 ]; then
   echo " !!! Database files not retrieved         "  | tee -a  $LOG_FILE
   exit_stat=1
   return 1
else
   echo " +++ Database files retrieved             "  | tee -a  $LOG_FILE
   if [ -f $HOME/rmfiles.sh ]; then
      chmod 777 $HOME/rmfiles.sh
      return 0
   fi
fi
cat $HOME/get_db_files.log >> $LOG_FILE
}

########################
start_inst()
########################
{
 echo>> $LOG_FILE
 echo " Starting  instance $ORACLE_SID...              " | tee -a  $LOG_FILE
 echo>> $LOG_FILE

$ORACLE_HOME/bin/sqlplus '/ as sysdba' <<STR 1> $HOME/startdb.log 2> $HOME/startdb.log
    startup 
    exit 

STR
 
rc=$?
st=`cat $HOME/startdb.log | grep "ORA-"`
if [ -n "$st" -o $rc -ne 0 ]; then
   echo " !!! Could not start instance $ORACLE_SID        "  | tee -a  $LOG_FILE
   exit_stat=1
   return 1
else
   echo " +++ Instance $ORACLE_SID started                "  | tee -a  $LOG_FILE
   return 0
fi
cat $HOME/startdb.log >> $LOG_FILE

}

########################
stop_inst()
########################
{
 echo>> $LOG_FILE
 echo " Stopping instance $ORACLE_SID ...              " | tee -a  $LOG_FILE
 echo>> $LOG_FILE

$ORACLE_HOME/bin/sqlplus '/ as sysdba' <<STP 1> $HOME/stopdb.log 2> $HOME/stopdb.log
    shutdown abort 
    exit 

STP

rc=$?
st=`cat $HOME/stopdb.log | grep "ORA-"`
if [ -n "$st" -o $rc -ne 0 ]; then
   echo " !!! Could not shutdown instance $ORACLE_SID     "  | tee -a  $LOG_FILE
   exit_stat=1
   return 1
else
   echo " +++ Instance stopped                            "  | tee -a  $LOG_FILE
   return 0
fi
cat $HOME/stopdb.log >> $LOG_FILE
#
}

##############################
stop_listener()
##############################
{
 echo>> $LOG_FILE
 echo " Stopping the listener LSTNR_$ORACLE_SID ...    " | tee -a  $LOG_FILE
 echo>> $LOG_FILE

$ORACLE_HOME/bin/lsnrctl  <<LSN 1> $HOME/stoplsnr.log 2> $HOME/stoplsnr.log
   stop LSTNR_$ORACLE_SID
   exit 

LSN
 
rc=$?
st=`cat $HOME/stoplsnr.log | grep "ORA-"`
if [ -n "$st" -o $rc -ne 0 ]; then
   echo " !!! Could not stop listener LSTNR_$ORACLE_SID   "  | tee -a  $LOG_FILE
   exit_stat=1
   return 1
else
   echo " +++ Listener stopped                            "  | tee -a  $LOG_FILE
fi
cat $HOME/stopdb.log >> $LOG_FILE
}

##############################
delete_db_files()
##############################
{
 echo>> $LOG_FILE
 echo " Deleting database files ...                    " | tee -a  $LOG_FILE
 echo>> $LOG_FILE
 ($HOME/rmfiles.sh > $uninstall_dir/rmfiles.log) 1>>$LOG_FILE 2>> $LOG_FILE
 if [ $? -ne 0 ]; then
 echo " !!! Could not delete database files - for details look at $uninstall_dir/rmfiles.log"  | tee -a  $LOG_FILE
 exit_stat=1
    exit 1
 else 
 echo " +++ Database files deleted                        "  | tee -a  $LOG_FILE
 fi 
}

##############################
check_cmd()
##############################
{
  rc=$?
  if [ $rc -ne 0 ]; then
     echo "!!! Failed executing command $1 " | tee -a  $LOG_FILE
     exit_stat=1
  fi
}

echo_n()
{
if [ -z "`echo -n`" ]; then
    echo -n "$1"
else
    echo "$1\c"
fi
}


##############################
# main 
##############################
ans=N
echo 
echo_n "Are you sure you want to uninstall Oracle ? (Y/N) "
read ans
if [ "$ans" != "Y" -a "$ans" != "y" ]; then
	exit
fi

install_type=Client
ORACLE_SID=odsbcidb
ORACLE_HOME=/home/ap/ctem/oracle/product/10.1.0.2
export ORACLE_HOME ORACLE_SID

# create Uninstall dir
uninstall_dir=$HOME/UNINSTALL
if [ -d $uninstall_dir ]; then
	rm -rf $uninstall_dir
fi
mkdir $uninstall_dir

LOG_FILE=$HOME/uninstall_oracle.log
if [ -f $LOG_FILE ]; then
   rm -f $LOG_FILE
fi
echo 'LOG_FILE = ' $LOG_FILE

# check if the unix user performing the uninstall is the owner of the files
current_user=`id | awk -F\( '{print $2}' | awk -F\) '{print $1}'`
current_dir=`dirname $0`
file_owner=`ls -ld $current_dir | awk '{print $3}'`
if [ "$current_user" != "$file_owner" ]; then
   echo 
   echo " !!! User $current_user is not the installation's owner." | tee -a  $LOG_FILE
   echo " !!! Please logon as "$file_owner" and restart          " | tee -a  $LOG_FILE
   sleep 4
   exit 1
   echo 
fi

# backup tmp and log dir
if [ -d $HOME/tmp ]; then
   if [ -d $uninstall_dir/tmp ]; then
      rm -rf  $uninstall_dir/tmp
   fi
   mv $HOME/tmp $uninstall_dir/tmp
fi

if [ -d $HOME/log ]; then
   if [ -d $uninstall_dir/log ]; then
      rm -rf  $uninstall_dir/log
   fi
   mv $HOME/log $uninstall_dir/log
fi

# Check if a server or a client has been installed.
exit_stat=0
if [ "$install_type" = "Server" ]; then
   echo>> $LOG_FILE
  echo "################################################" >> $LOG_FILE
   echo " About to uninstall oracle server and files ..." | tee -a  $LOG_FILE
  echo "################################################" >> $LOG_FILE
   echo>> $LOG_FILE
   uninstall_server
   if [ $exit_stat -eq 0 ]; then
      echo "+++ Uninstall of oracle software and database $ORACLE_SID " | tee -a  $LOG_FILE
      echo "+++ has been successfully completed.                      " | tee -a  $LOG_FILE
   else
       echo "!!! Uninstall of oracle software and database $ORACLE_SID " | tee -a  $LOG_FILE
       echo "!!! has not succeeded. Please see error in $LOG_FILE.     " | tee -a  $LOG_FILE 
   fi
   exit $exit_stat
   
else
   echo>> $LOG_FILE
  echo "################################################" >> $LOG_FILE
   echo " About to uninstall oracle client and files ..." | tee -a  $LOG_FILE
  echo "################################################" >> $LOG_FILE
   echo>> $LOG_FILE
   uninstall_files
   if [ $exit_stat -eq 0 ]; then
      echo "+++ Uninstall of oracle software has been successfully completed. " | tee -a  $LOG_FILE
   else
       echo "!!! Uninstall of oracle software has not succeeded. " | tee -a  $LOG_FILE
       echo "!!! Please see error in $LOG_FILE.                  " | tee -a  $LOG_FILE 
   fi
   exit $exit_stat
fi   

